package com.yizn.learn.concurrent;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * cyclicbarrier的例子
 *
 * @author yisl
 * @create 2020-08-29 16:00
 */
public class CyclicBarrierTest {
    private static CyclicBarrier cyclicBarrier;

      static class CyclicBarrierThread extends Thread{
        @Override
        public void run(){
            System.out.println(Thread.currentThread().getName() + "到了");
            try {
                cyclicBarrier.await();
                System.out.println(Thread.currentThread().getName() + "走了");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        cyclicBarrier = new CyclicBarrier(5, new Runnable() {
            public void run() {
                System.out.println("校长到了，开始开会");
            }
        });

        for (int i=0; i<5; i++){
            new CyclicBarrierThread().start();
        }
    }
}
